package cn.edu.zjicm.wordsnet_d.activity;

import java.util.Calendar;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.adapter.CalendarPagerAdapter;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.interfaces.CalendarActivityCallback;
import cn.edu.zjicm.wordsnet_d.synchronize.PunchInterface;
import cn.edu.zjicm.wordsnet_d.synchronize.UserInterface;
import cn.edu.zjicm.wordsnet_d.util.DateTimeUtils;
import cn.edu.zjicm.wordsnet_d.view.CalendarView.CalendarCallback;
import cn.edu.zjicm.wordsnet_d.view.CalendarView.State;

public class PunchOutCalendarActivity extends WordBaseActivity implements OnClickListener, CalendarCallback {

	public static void startActivity(Context context) {
		context.startActivity(new Intent(context, PunchOutCalendarActivity.class));
	}

	private TextView monthTv, yearTv, punchOutTimesTv;
	private ViewPager mViewPager;
	private ImageView toPreMonth, toNextMonth;

	private CalendarPagerAdapter calendarPagerAdapter;
	private CalendarActivityCallback calendarActivityCallback;
	private ProgressDialog dialog2 = null;

	private int realDate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_punch_out_calendar);
		initTitle();
		findView();
		initView();
		if (AppPreference.getLoginID(this) != "guest" && AppPreference.getLoginID(this) != "")
			if (!AppPreference.isDownloadPunchData(this))
				getPunch();
	}

	private void initTitle() {
		setTitleBarStyle(TITLE_BAR_STYLE_BACK_BUTTON);
		setTitleText("打卡日历");
		setRightButton1("", R.drawable.calendar_info_selector, clickListener);
	}

	OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			ShowRegularActivity.startActivity(PunchOutCalendarActivity.this, ShowRegularActivity.TYPE_PUNCH_OUT);
		}
	};

	private void initView() {
		calendarPagerAdapter = new CalendarPagerAdapter(getSupportFragmentManager(), this);
		mViewPager.setAdapter(calendarPagerAdapter);
		mViewPager.setOnPageChangeListener(new PageChangeListener());
		mViewPager.setCurrentItem(DateTimeUtils.getTodayMonthIndex(Calendar.getInstance()));
		updateShowDate(DateTimeUtils.getTodayMonthIndex(Calendar.getInstance()));
		toPreMonth.setOnClickListener(this);
		toNextMonth.setOnClickListener(this);
		setPunchOutDay();
	}

	private void setPunchOutDay() {
		punchOutTimesTv.setText("总共打卡天数：" + WordFactory.getInstance(this).getPunchOutTimes() + "天");
	}

	private void findView() {
		monthTv = (TextView) findViewById(R.id.show_month_view);
		yearTv = (TextView) findViewById(R.id.show_year_view);
		mViewPager = (ViewPager) findViewById(R.id.viewpager);
		toPreMonth = (ImageView) findViewById(R.id.toPreMonth);
		toNextMonth = (ImageView) findViewById(R.id.toNextMonth);
		punchOutTimesTv = (TextView) findViewById(R.id.allPunchOutTimes);
	}

	class PageChangeListener extends ViewPager.SimpleOnPageChangeListener {
		@Override
		public void onPageSelected(int position) {
			updateShowDate(position);
		}
	}

	private void updateShowDate(int position) {
		yearTv.setText(DateTimeUtils.getMinYear() + (position / 12) + "");
		int month = (position % 12) + 1;
		monthTv.setText(DateTimeUtils.getEnglishMonth(month));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.toPreMonth:
			mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
			break;

		case R.id.toNextMonth:
			mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
			break;
		}
	}

	protected void getPunch() {
		getPunchHandler.sendMessage(getPunchHandler.obtainMessage(UserInterface.MSG_START));
		PunchInterface.getInstance(this).getPunch(getPunchHandler);
	}

	Handler getPunchHandler = new Handler() {
		ProgressDialog dialog2 = null;

		public void handleMessage(android.os.Message msg) {
			if (msg.what != UserInterface.MSG_START)
				if (dialog2 != null) {
					dialog2.dismiss();
					dialog2 = null;
				}

			switch (msg.what) {
			case UserInterface.MSG_START:
				dialog2 = ProgressDialog.show(PunchOutCalendarActivity.this, null, "正在获取打卡数据...");
				break;
			case UserInterface.MSG_SUCCESS:
				Toast.makeText(PunchOutCalendarActivity.this, "获取打卡数据成功", Toast.LENGTH_SHORT).show();
				initView();
				break;
			case UserInterface.MSG_UNSUCCESS:
				Toast.makeText(PunchOutCalendarActivity.this, "获取打卡数据失败", Toast.LENGTH_SHORT).show();
				break;
			case UserInterface.MSG_BAD_NETWORK:
				Toast.makeText(PunchOutCalendarActivity.this, UserInterface.MSG_BAD_NETWORK_STRING, Toast.LENGTH_SHORT).show();
				break;
			case UserInterface.MSG_WEB_SITE_ACCESS_FAILURE:
				Toast.makeText(PunchOutCalendarActivity.this, UserInterface.MSG_WEB_SITE_ACCESS_FAILURE_STRING, Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
		};
	};

	private State animationState;

	@Override
	public void clickToPunch(State state, CalendarActivityCallback calendarActivityCallback) {

		this.calendarActivityCallback = calendarActivityCallback;
		this.animationState = state;

		if (!UserInterface.getInstance(this).isLogin()) {
			calendarActivityCallback.resetCell(state);
			RegisterActivity.startActivity(this, RegisterActivity.TO_CALENDAR);
		} else {
			rePunchHandler.sendEmptyMessage(UserInterface.MSG_START);
			PunchInterface.getInstance(this).punch(rePunchHandler, state);
		}

	}

	Handler rePunchHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			if (msg.what != UserInterface.MSG_START) {
				if (dialog2 != null) {
					dialog2.dismiss();
					dialog2 = null;
				}
			}

			switch (msg.what) {
			case UserInterface.MSG_START:
				dialog2 = ProgressDialog.show(PunchOutCalendarActivity.this, null, "正在补打卡...");
				break;
			case UserInterface.MSG_SUCCESS:
				int date = msg.arg1;
				Toast.makeText(PunchOutCalendarActivity.this, "补打卡成功", Toast.LENGTH_SHORT).show();
				AppPreference.removeRePunchDate(PunchOutCalendarActivity.this, animationState);
				calendarActivityCallback.clickAnimation(animationState, date);
				setPunchOutDay();
				break;
			case UserInterface.MSG_UNSUCCESS:
				calendarActivityCallback.resetCell(animationState);
				Toast.makeText(PunchOutCalendarActivity.this, "补打卡失败", Toast.LENGTH_SHORT).show();
				break;
			case UserInterface.MSG_BAD_NETWORK:
				calendarActivityCallback.resetCell(animationState);
				Toast.makeText(PunchOutCalendarActivity.this, "补打卡失败," + UserInterface.MSG_BAD_NETWORK_STRING, Toast.LENGTH_SHORT).show();
				break;
			case UserInterface.MSG_WEB_SITE_ACCESS_FAILURE:
				calendarActivityCallback.resetCell(animationState);
				Toast.makeText(PunchOutCalendarActivity.this, "补打卡失败," + UserInterface.MSG_WEB_SITE_ACCESS_FAILURE_STRING, Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
		};
	};

	@Override
	public void setCurrentItem(int realDate) {
		String s_realDate = "20" + realDate + "";
		int realYear = Integer.valueOf(s_realDate.substring(0, 4)); // 未打卡的年月日
		int realMonth = Integer.valueOf(s_realDate.substring(4, 6));
		int realDay = Integer.valueOf(s_realDate.substring(6, 8));

		Calendar calendar = Calendar.getInstance();
		calendar.set(realYear, realMonth - 1, realDay);

		calendarPagerAdapter.notifyDataSetChanged();

		mViewPager.setCurrentItem(DateTimeUtils.getTodayMonthIndex(calendar));
	}
}
